<?php
class Admin extends Kernel {

    public $menu = array(
        'main' => '首页',
        'setting' => '系统设置',
        'nav' => '导航',
        'sort' => '分类',
        'article' => '文章',
        'comment' => '评论',
        'user' => '用户',
        //'theme' => '模板',
        'plugin' => '插件',
        'link' => '友情链接',
        'database' => '数据库',
    );
    public $admin = null;

    public function __construct() {
        parent::__construct();
        $this->v->init( APP_PATH . "/themes/admin/", APP_TPL . "admin/" );
        $this->chkLogin();
        $this->themesURL = $this->setting[ 'weburl' ] . "/themes/admin";
    }

    /*
     * 检查是否登录
     */
    private function chkLogin() {
        if ( $this->admin = $this->isLogin() ) {
            $group = $this->sort( 'user' );
            if ( !isset( $group[ $this->admin[ 'sid' ] ] ) or $group[ $this->admin[ 'sid' ] ][ 'alias' ] != 'system' ) {
                $this->login();
                return;
            }
            return;
        }
        $this->login();
    }

    /*
     *  拼接get参数
     */
    public function get( $arr = null ) {
        $attr = $_GET;
        array_shift( $attr );
        if ( is_string( $arr ) ) {
            if ( $tmp = explode( '-', $arr ) ) {
                foreach ( $tmp as $v ) {
                    unset( $attr[ $v ] );
                }
            } else {
                unset( $attr[ $arr ] );
            }
            return $attr;
        }
        return $arr == null ? array_unique( $attr ) : array_unique( array_merge( $attr, $arr ) );
    }

    /**
     * 错误提示程序
     *
     * @param $msg     错误提示需要的相关信息
     * @param $url     跳转地址
     * @param $delay   延迟时间
     */
    public function error( $msg = null, $url = null, $delay = 5 ) {
        if ( R( 'ajax' ) ) exit( $msg );
        if ( $msg == null ) $msg = "请求参数错误!" . S( 'REQUEST_URI' );
        if ( $url == null ) $url = $this->url( R( 'c' ) );
        include $this->display( "info" );
        exit;
    }

    /**
     * 成功提示程序
     *
     * @param $msg     成功提示需要的相关信息
     * @param $url     跳转地址
     * @param $delay   延迟时间
     */
    public function success( $msg, $url = null, $delay = 5 ) {
        if ( R( 'ajax' ) ) exit( "success" );
        if ( $url == null ) $url = $this->url( R( 'c' ) );
        include $this->display( "info" );
        exit;
    }

    /*
     * 返回输入框下拉列表格式
     */
    public function tag_list( $limit = null ) {
        $tags = $this->model->tag_limit( $limit );
        $tag_list = '';
        foreach ( $tags as $tag ) {
            $tag_list .= "\"{$tag['tag']}\",";
        }
        return rtrim( $tag_list, ',' );
    }

    /*
     *  URL模式的构建函数
     *
     * @param @controller    控制器名称
     * @param @action    动作名称
     * @param @args    传递的参数，数组形式
     */
    public function url( $controller = null, $action = null, $args = null ) {

        if ( $controller == null ) return $this->setting[ 'weburl' ] . $GLOBALS[ "url_path_base" ];

        $url = $GLOBALS[ "url_path_base" ] . "?c={$controller}";

        if ( $action != null ) $url .= "&a={$action}";
        if ( null != $args ) foreach ( $args as $key => $arg ) $url .= "&{$key}={$arg}";
        return $this->setting[ 'weburl' ] . $url;
    }

    private function login() {
        if ( R( 'c' ) !== 'login' ) {
            $referer = S( 'REQUEST_URI' );
            $referer = strpos( $referer, "?" ) === false ? null : array( 'referer' => urlencode( $referer ) );
            $login_url = $this->url( "login", null, $referer );
            header( "location:{$login_url}" );
            exit;
        }
    }
}